/*************************************************************************
	> File Name: B3630.cpp
	> Author: yufangze
	> Mail: 2224590123@qq.com
	> Created Time: 2025-01-10 13:29:39
 ************************************************************************/

#include <iostream>
#include <set>
#include <map>
#include <vector>
#include <algorithm>
using namespace std;

#define MAX_N 1000000
int mp[MAX_N + 5];

int main() {
    int n;
    cin >> n;
    for (int i = 1, x; i <= n; i++) {
        cin >> x;
        mp[i] = x;
    }
    int start;
    cin >> start;
    while (start) {
        cout << start << " ";
        start = mp[start];
    }
    cout << endl;
    return 0;
}
